﻿
namespace ServerSide.RIA.Web
{
    using System;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using ServerSide.Common.Helpers;
    using System.Reflection;
    using ServerSide.Common.Types;
    using ServerSide.Common;
    using System.ServiceModel.Activation;
    using System.Web;


    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EOrganizatorDomainService : DomainService
    {
        public void TypeAdderFunction(
            ResponseMessage b1,
            Common.Requests.LoginUserRequest lreq,
            Common.Requests.LoginUserResult lres,
            Common.Requests.LogoutUserRequest logoutreq,
            Common.Requests.LogoutUserResult logoutRes,
            Requests.HelloRequest h,
            Requests.HelloRequestResult r,
            Requests.MeyvelistesiRequest r1,
            Requests.MeyveListesiResult r2
            )
        { return; }


        [Invoke]
        public ServerSide.Common.ResponseMessage DoWork(RequestMessage requestMessage)
        {
            ResponseMessage response = new ResponseMessage();

            // Request belirleniyor
            Assembly RequestsAssembly = Assembly.Load(new System.Reflection.AssemblyName("ServerSide.Requests"));
            Type RequestType = RequestsAssembly.GetType(requestMessage.RequestTypeName);

            // bu request common tarafındaki requestlerden olabilir.
            if (RequestType == null)
            {
                RequestsAssembly = Assembly.Load(new System.Reflection.AssemblyName("ServerSide.Common"));
                RequestType = RequestsAssembly.GetType(requestMessage.RequestTypeName);
            }

            var request = Serializer.StringToObject(requestMessage.SerializedRequest, RequestType);

            if (!IsRequestIsAClientRequest(request))
            {
                response.ResponseType = ResponseType.ErrorOccured;
                response.Message = "Gelen istek bir request değil";
                return response;
            }

            IRequest req = (IRequest)request;
            if (IsRequestNeedsLogin(req))
            {
                if (!IsUserLoggedIn())
                {
                    response.ResponseType = ResponseType.NeedLogin;
                    response.Message = "Lütfen giriş yapınız";
                    return response;
                }
                // tamamdır
            }

            // Request çalıştırılarak result alınıyor'u alınıyor

            //object requestResult = RequestType.InvokeMember("Process", BindingFlags.InvokeMethod, null, request, null);
            object requestResult = req.Process();

            response.ResponseType = ResponseType.Succes;
            response.SerializedRequestResult = Serializer.ObjectToString(requestResult);

            return response;
        }

        private bool IsUserLoggedIn()
        {
            return (HttpContext.Current.Session[Common.Types.Constants.SESSION_TICKET_KEY_NAME] != null);
        }

        private bool IsRequestIsAClientRequest(object req)
        {
            return
                req is IRequest // Irequest olacak  
                && req.GetType().IsDefined(typeof(Request), false); // clientRequest attribute unu alacak
        }

        private bool IsRequestNeedsLogin(IRequest req)
        {
            string s = req.GetType().ToString();

            Attribute[] attrs = Attribute.GetCustomAttributes(req.GetType());

            foreach (var item in attrs)
            {
                if (item is Request)
                {
                    Request lgnAttr = (Request)item;
                    return lgnAttr.IsRequestNeedLogin;
                }
            }
            return false;
        }
    }
}


